//
//  CQFootTableView.m
//  HiChat
//
//  Created by wqiong on 16/1/13.
//  Copyright © 2016年 wqiong. All rights reserved.
//

#import "CQFootTableView.h"

@interface CQFootTableView () <UITextFieldDelegate>

@end

@implementation CQFootTableView

- (void)dealloc
{
    [_recordButton release];
    [_textField release];
    [_voiceButton release];
    [_backgroundImageV release];
    [_photoButton release];
    [_addButton release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatSubviews];
    }
    return self;
}

- (void)creatSubviews
{
    self.backgroundColor = [UIColor orangeColor];
    self.backgroundImageV = [[CQBaseImageView alloc] init];
    self.backgroundImageV.frame = self.bounds;
    self.backgroundImageV.image = [UIImage imageNamed:@""];
    self.backgroundImageV.backgroundColor = [UIColor grayColor];
    
    self.textField = [[CQBaseTextField alloc] init];
    self.textField.delegate = self;
    self.textField.layer.borderWidth = 0.5;
    [self addSubview:self.textField];
    
    self.recordButton = [CQBaseButton buttonWithType:UIButtonTypeCustom];
    [self.recordButton setTitle:@"按住开始录制" forState:UIControlStateNormal];
    [self.recordButton setBackgroundColor:[UIColor cyanColor]];
    [self.recordButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.recordButton addTarget:self action:@selector(recordAction:) forControlEvents:UIControlEventTouchDown];
    [self.recordButton addTarget:self action:@selector(recordOverAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.recordButton addTarget:self action:@selector(recordOutAction:) forControlEvents:UIControlEventTouchDragOutside];
    self.recordButton.layer.borderWidth = 0.5;
    [self.recordButton setHidden:YES];
    [self addSubview:self.recordButton];
    
    self.voiceButton = [CQBaseButton buttonWithType:UIButtonTypeCustom];
    [self.voiceButton setBackgroundImage:[UIImage imageNamed:@"chatBar_record@2x.png"] forState:UIControlStateNormal];
    [self.voiceButton addTarget:self action:@selector(voiceAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.voiceButton];
    
    self.photoButton = [CQBaseButton buttonWithType:UIButtonTypeCustom];
    
    [self.photoButton setBackgroundImage:[UIImage imageNamed:@"chatBar_more@2x.png"] forState:UIControlStateNormal];
    [self.photoButton addTarget:self action:@selector(photoAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.photoButton];
    
    self.addButton = [CQBaseButton buttonWithType:UIButtonTypeCustom];
    self.addButton.backgroundColor = [UIColor blackColor];
    [self addSubview:self.addButton];
    
}

- (void)voiceAction:(UIButton *)sender {
    if (sender.selected == NO) {
        [self.voiceButton setBackgroundImage:[UIImage imageNamed:@"chatBar_keyboardSelected@2x.png"] forState:UIControlStateNormal];
        [self.textField setHidden:YES];
        [self.textField resignFirstResponder];
        sender.selected = YES;
        [self.recordButton setHidden:NO];
    } else {
        [self.voiceButton setBackgroundImage:[UIImage imageNamed:@"chatBar_record@2x.png"] forState:UIControlStateNormal];
        [self.textField becomeFirstResponder];
        [self.textField setHidden:NO];
        [self.recordButton setHidden:YES];
        sender.selected = NO;
    }
    
}

- (void)recordAction:(UIButton *)sender {
    [sender setBackgroundColor:[UIColor grayColor]];
    [self.recordButton setTitleColor:[UIColor cyanColor] forState:UIControlStateNormal];
    sender.selected = YES;
    [self.delegate voiceDelegate:sender];
}

- (void)recordOverAction:(UIButton *)sender {
    [sender setBackgroundColor:[UIColor cyanColor]];
    [self.recordButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    sender.selected = NO;
    [self.delegate voiceDelegate:sender];
}
- (void)recordOutAction:(UIButton *)sender {
    [sender setBackgroundColor:[UIColor cyanColor]];
    [self.recordButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    sender.selected = NO;
    [self.delegate voiceDelegate:sender];
}

- (void)photoAction:(UIButton *)sender {
    [self.textField resignFirstResponder];
    [self.delegate addDelegate:sender];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self.voiceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self).with.offset(5);
        make.width.equalTo(self.voiceButton.mas_height).with.offset(0);
        make.bottom.equalTo(self.mas_bottom).with.offset(-5);
    }];
    
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.voiceButton.mas_right).offset(5);
        make.top.bottom.equalTo(self.voiceButton);
        make.right.equalTo(self.photoButton.mas_left).offset(-5);
    }];
    
    [self.recordButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.voiceButton.mas_right).offset(5);
        make.top.bottom.equalTo(self.voiceButton);
        make.right.equalTo(self.photoButton.mas_left).offset(-5);
    }];
    
    [self.photoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.textField);
        make.width.height.equalTo(self.voiceButton).offset(0);
        make.right.equalTo(self.addButton.mas_left).offset(-5);
    }];
    
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.voiceButton);
        make.width.height.equalTo(self.voiceButton).offset(0);
        make.right.equalTo(self.mas_right).with.offset(-5);
    }];
    
    
}



- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    //增加监听，当键盘出现或改变时收出消息
    [_textField becomeFirstResponder];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    //增加监听，当键退出时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    
    _textField.returnKeyType = UIReturnKeySend;

}
//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification
{
    //获取键盘的高度
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    
    [self.delegate ChangeViewHeight:keyboardRect.size.height];
}

//当键盘退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification {
    //获取键盘的高度
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    [self.delegate returnViewHeight:keyboardRect.size.height];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self.fieldDelegate sendMessage:textField.text];
     [_textField resignFirstResponder];
    
    
    
    _textField.text = nil;
    return YES;
}
@end
